using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SAInterfaceLibrary;

namespace RootBuilderConfigControls
{
    /// <summary>
    /// Control to display and let user modify a particular config setting.
    /// </summary>
    public partial class BuildSpecUI : UserControl
    {
        public BuildSpecUI()
        {
            InitializeComponent();
        }

        private ProjectBuildConfigSpec _spec;

        /// <summary>
        /// The spec we are representing and modifying.
        /// </summary>
        public ProjectBuildConfigSpec BuildSpec
        {
            get
            {
                LoadSpecFromUI();
                return _spec;
            }
            set
            {
                _spec = value;
                UpdateUIFromSpec();
            }
        }

        /// <summary>
        /// Make sure the UI and the spec are in tune!
        /// </summary>
        private void LoadSpecFromUI()
        {
            _spec.Name = tbName.Text;
            _spec.ConfigArguments = tbConfigArguments.Text;
        }

        /// <summary>
        /// We have a new spec. Reset everything!
        /// </summary>
        private void UpdateUIFromSpec()
        {
            if (_spec == null)
            {
                _spec = new ProjectBuildConfigSpec();
            }
            tbName.Text = _spec.Name;
            tbConfigArguments.Text = _spec.ConfigArguments;
        }

        /// <summary>
        /// User wants to save the thing!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bSave_Click(object sender, EventArgs e)
        {
            if (tbName.Text.Length == 0)
            {
                MessageBox.Show("You must specify a config name");
                return;
            }

            LoadSpecFromUI();
            _spec.Save();
        }
    }
}